package com.example.sxproject.controller;

import com.example.sxproject.pojo.Card;
import com.example.sxproject.pojo.Result;
import com.example.sxproject.pojo.User;
import com.example.sxproject.service.CardService;
import com.example.sxproject.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CardService cardService;

    private static String UPLOADED_FOLDER = "src/main/resources/templates/img/";

    private static org.apache.log4j.Logger logger = Logger.getLogger(UserController.class);

    //登录
    @RequestMapping("/user/login")
    public String login(String phone, String password, HttpSession session) {


        MDC.put("changeModule", "login"); // 设置修改模块
        MDC.put("locateUrl", "/user/login"); // 设置资源路径
        MDC.put("changeStatus", "1"); // 设置修改状态
        MDC.put("userId", 0);

        logger.info("尝试登录，手机号"+phone);

        MDC.remove("userId");
        MDC.remove("changeModule");
        MDC.remove("locateUrl");
        MDC.remove("changeStatus");



        User user = userService.findByPhone(phone);//根据用户名查询用户

        // 判断用户是否存在
        if (user == null) {
            session.setAttribute("msg", "用户名不存在！");
            return "login"; // 返回登录页，并提示用户名不存在
        }

        session.setAttribute("user", user);// 把内容存到session中
        if (user.getPassword().equals(password)) {
            if (user.getRealName() == null || user.getRealName().equals("")) {
                session.setAttribute("msg", "请先完善个人信息！");
                return "myInformation";
            }
            MDC.put("changeModule", "login"); // 设置修改模块
            MDC.put("locateUrl", "/user/login"); // 设置资源路径
            MDC.put("changeStatus", "1"); // 设置修改状态
            MDC.put("userId", user.getId());

            logger.info("登录成功：账号"+phone);

            MDC.remove("changeModule");
            MDC.remove("locateUrl");
            MDC.remove("changeStatus");
            MDC.remove("userId");
            return "index";
        }
        else { //密码错误
            session.setAttribute("msg", "密码错误！");
            return "login";
        }
    }

    @PostMapping("/user/register")
    public String register(@RequestParam String phone, String password, String remPassword, HttpSession session) {

        if (phone == null || password == null || remPassword == null) {
            session.setAttribute("msg", "用户名或密码不可为空！");
            return "register";
        }

        //2次密码不一致
        if (!password.equals(remPassword)) {
            session.setAttribute("msg", "两次输入的密码不一致！");
            return "register";
        }
        //查询用户
        User user = userService.findByPhone(phone);

        if (user == null) {
            //没有占用
            //注册
            userService.register(phone, password, 1);
            return "login";
        } else {
            //用户名已经被占用
            session.setAttribute("msg", "用户名已被占用！");
            return "register";
        }
    }


    //修改个人信息
    @PostMapping("/user/updateinformation")
    public String updateInformation(User user, HttpSession session) {

        User u = (User) session.getAttribute("user");

        user.setPhoto(u.getPhoto());

        userService.updateInformation(user); //更新用户信息
        User updatedUser = userService.findByPhone(user.getPhone());
        session.setAttribute("user", updatedUser);

        return "changedInformation";
    }

    //更新：更改登录密码
    @PutMapping("/changepassword")
    public Result changePassword(@RequestParam String phone, @RequestParam String password) {
        userService.changePassword(phone, password);
        return Result.success();
    }

    //更新：实现假删除
    /*@PutMapping("/deleteuser")
    public Result deleteUser(@RequestParam String phone, @RequestParam Integer userStatus) {
        userService.deleteUser(id);
        return Result.success();
    }
*/

    @GetMapping("/user/checkHouseNumber")
    //查询用户表houseNumber是否存在
    public Result checkHouseNumberExists(String houseNumber) {
        User h = userService.findByHouseNumber(houseNumber);
        if (h == null) {
            return Result.error(("用户不存在"));
        } else {
            return Result.success();
        }
    }
}





